package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import persistence.NotesEngineDAO;
import persistence.NotesVO;
import persistence.PerformanceEngineDAO;
import persistence.PerformanceVO;

/**
 * Servlet implementation class getquotes
 */
public class notes extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public notes() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
		NotesEngineDAO dao = new NotesEngineDAO();
		NotesVO vo = new NotesVO();
		vo.setDateStock(request.getParameter("date"));
		vo.setName(request.getParameter("notesname"));
		vo.setTag(request.getParameter("tagname"));
		vo.setDateAlert(request.getParameter("alert"));
		vo.setBody(request.getParameter("note"));
		vo.setStock(request.getParameter("stock"));

		if(request.getParameter("action").equals("new")){
			dao.persist(vo);
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?id=");
		}

		if(request.getParameter("action").equals("update")) {
			dao.update(vo);
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?id="+request.getParameter("id"));
		}		

		if(request.getParameter("action").equals("delete")){
			dao.delete(Integer.parseInt(request.getParameter("id")));
			response.sendRedirect("/stockblog/pages/"+request.getParameter("page")+".jsp?id="+request.getParameter("date"));
		}		
	}

}
